﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.VisualBasic;
using System.Web.Security;
namespace Quality.BasicData
{
    public partial class users : Form
    {
       
        public users()
        {
            InitializeComponent();
            this.dataGridView1.AutoGenerateColumns = false;
        }

        private void users_Load(object sender, EventArgs e)
        {
            sqlHelper.setTreeView(treeView1, 0);

          

        }
        private void userlist() {


            if (treeView1.SelectedNode == null)
            {

                return;
            }

                string s = "";
                DiGui(treeView1.SelectedNode,ref s);
                s = s.TrimEnd(',');
                string sql = "select * from users a left join organization b on a.org=b.id where a.org in (" + s + ") ";
                DataTable ds = sqlHelper.ExecuteDataTable(sql);
                dataGridView1.DataSource = ds;

        
        }
        private void DiGui(TreeNode tn,ref string s)
        {

            s += tn.Tag+",";
            foreach (TreeNode tnSub in tn.Nodes)
            {
                DiGui(tnSub,ref s);
            }
        }
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            userlist();
        }

        private void newtoolStripButton_Click(object sender, EventArgs e)
        {
            string id = "";
            usersedit frm = new usersedit(id);

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.StartPosition = FormStartPosition.Manual;
            frm.Location = new Point(10, 10);
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent()
        {
            userlist();

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            string id = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); 
            usersedit frm = new usersedit(id);

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }

        private void deltoolStripButton_Click(object sender, EventArgs e)
        {
            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            DialogResult dr = MessageBox.Show("确定要删除吗?", "删除", messButton);
            if (dr == DialogResult.OK)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {

                    SqlCommand comm = new SqlCommand();
                    comm.CommandText = "delete from users where id=@id";
                    comm.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
                    DB.dosql(comm);
                    frm_TransfEvent();

                }
            }
        }

        private void searchtoolStripButton_Click(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = new SqlCommand();
            if (keytoolStripTextBox.Text == "")
            {

                da.SelectCommand.CommandText = "select top 1000 a.id,a.userlogin,a.username,b.nodeName,a.telephone,case when a.stop=1 then '停用' end stop from users a left join organization b on a.org=b.id ";

            }
            else
            {

                da.SelectCommand.CommandText = "select a.id,a.userlogin,a.username,b.nodeName,a.telephone,case when a.stop=1 then '停用' end stop from users a left join organization b on a.org=b.id where a.username like '%" + keytoolStripTextBox.Text.Trim() + "%' ";
                

            }
            DataSet dt = DB.getlist(da);
            dataGridView1.DataSource = dt.Tables[0];
        }

        private void passtoolStripButton_Click(object sender, EventArgs e)
        {
             string str = Interaction.InputBox("输入密码","标题","",-1,-1);
                    str = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5");
             if (dataGridView1.SelectedRows.Count > 0)
             {

                 SqlCommand comm = new SqlCommand();
                 comm.CommandText = "update users set password=@password from users where id=@id";
                 comm.Parameters.AddWithValue("@password", str);
                 comm.Parameters.AddWithValue("@id", dataGridView1.CurrentRow.Cells[0].Value.ToString());
                 DB.dosql(comm);
                 frm_TransfEvent();

             }

        }

        
    }
}
